# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

# Create your views here.

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from live_api.tools.Output_Helper import *


# 频道列表
@csrf_exempt
def channellist(request):
    ret = ReturnJ()
    db =DB.getDB()
    try:
        info = INFO(request)
        info.checkPostEmpty('cur_page', 'pagesize')
        info.cur_page = str(int(info.cur_page) * int(info.pagesize))
        sql = sqlr("select * from channel limit #cur_page,#pagesize", info)
        n, rows = db.query(sql)
        ret.channellist = out_farmat_channel(rows)

    except Exception, e:
        ret.code = 1010
        ret.msg = str(e)
        # print(str(e))

    return HttpResponse(ret.toJson())


# 频道信息
@csrf_exempt
def channelvideo(request, id):
    ret = ReturnJ()
    try:
        db =DB.getDB()
        info = INFO(request)
        info.checkPostEmpty('cur_page', 'pagesize')
        info.cur_page = str(int(info.cur_page) * int(info.pagesize))
        info.id = id
        # print "info:",info
        sql = sqlr("select * from video where  par1=0 and id_actor in (select id_actor from actor_channel where id_channel=#id ) ORDER BY end_date DESC limit #cur_page,#pagesize", info)
        n, rows = db.query(sql)
        ret.channelvideo = out_farmat_video(rows)

    except Exception, e:
        ret.code = 1010
        ret.msg = str(e)

    return HttpResponse(ret.toJson())
